package com.lcj.demo;

import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class ProcessInstanceTest {

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义
	 */
	@Test
	public void deployment() {
		Deployment deployment = processEngine.getRepositoryService().createDeployment().name("流程定义").addZipInputStream(
				new ZipInputStream(this.getClass().getClassLoader().getResourceAsStream("diagrams/MyProcess.zip")))
				.deploy();
		System.out.println(deployment.getId());
		System.out.println(deployment.getName());
		System.out.println(deployment.getDeploymentTime());
		System.out.println("流程定义完成");
	}

	/**
	 * 启动流程实例
	 */
	@Test
	public void startProcess() {
		ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("helloworld");
		System.out.println(processInstance.getId());
		System.out.println(processInstance.getProcessDefinitionId());
		System.out.println(processInstance.getProcessDefinitionName());
	}

	/**
	 * 查看个人任务
	 */
	@Test
	public void findPersonTask() {
		String assignee = "ww";
		List<Task> list = processEngine.getTaskService().createTaskQuery().taskAssignee(assignee)
				.orderByTaskCreateTime().asc().list();
		if (list != null && list.size() > 0) {
			for (Task task : list) {
				System.out.println("任务ID:" + task.getId());
				System.out.println("任务名称:" + task.getName());
				System.out.println("任务的创建时间:" + task.getCreateTime());
				System.out.println("任务的办理人:" + task.getAssignee());
				System.out.println("流程实例ID：" + task.getProcessInstanceId());
				System.out.println("执行对象ID:" + task.getExecutionId());
				System.out.println("流程定义ID:" + task.getProcessDefinitionId());
				System.out.println("########################################################");
			}
		}
	}
	
	/**
	 * 完成我的任务
	 */
	@Test
	public void completTask() {
		String taskId = "7502";
		processEngine.getTaskService().complete(taskId);
		System.out.println("任务完成："+taskId);
	}
	
	/**
	 * 查看流程状态
	 */
	@Test
	public void showProcessStatu() {
		String processInstanceId = "25001";
		ProcessInstance singleResult = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
		System.out.println(singleResult);
	}
}
